#include "proxydialog.h"
#include "ui_proxydialog.h"

ProxyDialog::ProxyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProxyDialog)
{
    ui->setupUi(this);
    QSettings settings;
    ui->type_comboBox->setCurrentIndex( settings.value("proxy/type", 0).toInt() );
    ui->host_lineEdit->setText( settings.value("proxy/host").toString() );
    ui->port_lineEdit->setText( settings.value("proxy/port").toString() );
    ui->username_lineEdit->setText( settings.value("proxy/username").toString() );
    ui->password_lineEdit->setText( settings.value("proxy/password").toString() );
    setProxy();
}

ProxyDialog::~ProxyDialog()
{
    delete ui;
}

void ProxyDialog::setProxy()
{
    QNetworkProxy proxy;
    switch (ui->type_comboBox->currentIndex())
    {
    case 0:
        proxy.setType(QNetworkProxy::NoProxy);
        break;
    case 1:
        proxy.setType(QNetworkProxy::Socks5Proxy);
        proxy.setHostName(ui->host_lineEdit->text());
        proxy.setPort(ui->port_lineEdit->text().toInt());
        proxy.setUser(ui->username_lineEdit->text());
        proxy.setPassword(ui->password_lineEdit->text());
        break;
    case 2:
        proxy.setType(QNetworkProxy::HttpProxy);
        proxy.setHostName(ui->host_lineEdit->text());
        proxy.setPort(ui->port_lineEdit->text().toInt());
        break;
    }
    QNetworkProxy::setApplicationProxy(proxy);
}

void ProxyDialog::on_type_comboBox_currentIndexChanged(int index)
{
    switch (index)
    {
    case 0:
        ui->label_2->setEnabled(false);
        ui->label_3->setEnabled(false);
        ui->label_4->setEnabled(false);
        ui->label_5->setEnabled(false);
        ui->host_lineEdit->setEnabled(false);
        ui->port_lineEdit->setEnabled(false);
        ui->username_lineEdit->setEnabled(false);
        ui->password_lineEdit->setEnabled(false);
        break;
    case 1:
        ui->label_2->setEnabled(true);
        ui->label_3->setEnabled(true);
        ui->label_4->setEnabled(true);
        ui->label_5->setEnabled(true);
        ui->host_lineEdit->setEnabled(true);
        ui->port_lineEdit->setEnabled(true);
        ui->username_lineEdit->setEnabled(true);
        ui->password_lineEdit->setEnabled(true);
        break;
    case 2:
        ui->label_2->setEnabled(true);
        ui->label_3->setEnabled(true);
        ui->label_4->setEnabled(false);
        ui->label_5->setEnabled(false);
        ui->host_lineEdit->setEnabled(true);
        ui->port_lineEdit->setEnabled(true);
        ui->username_lineEdit->setEnabled(false);
        ui->password_lineEdit->setEnabled(false);
        break;
    }
}

void ProxyDialog::on_buttonBox_accepted()
{
    QSettings settings;
    settings.setValue("proxy/type", ui->type_comboBox->currentIndex());
    settings.setValue("proxy/host", ui->host_lineEdit->text());
    settings.setValue("proxy/port", ui->port_lineEdit->text());
    settings.setValue("proxy/username", ui->username_lineEdit->text());
    settings.setValue("proxy/password", ui->password_lineEdit->text());

    setProxy();
    emit proxyChanged();
}
